<template>
	<view class="searchPage">
		<!-- <u-navbar title="我的家" :placeholder="true" leftIcon=" " :bgColor="bgColor" :titleStyle="titleStyle"></u-navbar> -->
		<headerinfo></headerinfo>
		<view class="bg">
			<!-- <headertop :inv="5"></headertop> -->
			<backos title="回到首页"></backos>
		</view>
		<view class="width60" style="margin-top: 0;">
			<view class="form">
				<view class="search">
					<u-search height="37" bgColor="#fff" :actionStyle="actionStyle" shape="round" placeholder="请输入搜索关键字"
						:showAction="true" @search="searchSubmit" @custom="searchSubmit"
						v-model="query.searchContent"></u-search>

				</view>

				<view class="his" v-if="hislist.length > 0">
					<view class="title">
						<span>历史搜索</span>
						<i class="iconfont" @click="clearHis">&#xe74b;</i>
					</view>
					<view class="list">
						<span @click="copyValue(item)" v-for="(item, index) in hislist" :key="index">{{ item }}</span>
					</view>
				</view>
				<view class="moreSearchBox" v-show="showMoreSearch">
					<!-- 分类 -->
					<view class="his" v-if="tabTitle.length > 0">
						<view class="title"><span>分类</span><span @click="clearType"
								style="font-size: 28rpx;color: #929292;">清空</span></view>
						<view class="list">
							<span :class="item.checked ? 'active' : ''" @click="checkFenlei(index)"
								v-for="(item, index) in tabTitle" :key="index">{{ item.name }}</span>
						</view>
					</view>
					<view class="his">
						<view class="title">
							<span>日期范围</span>
							<span @click="clearDate" style="font-size: 28rpx;color: #929292;">清空</span>
						</view>
						<view class="list" @click="rangeVisible = true">
							<span class="dateSpan" style="margin-right: 0;">{{startDate?startDate:'开始日期'}}</span>
							<div class="zhi">至</div>
							<span class="dateSpan">{{endDate?endDate:'结束日期'}}</span>
						</view>
					</view>
					<!-- 家庭 -->
					<view class="his" v-if="familylist.length > 0">
						<view class="title"><span>家庭</span><span @click="clearFamily"
								style="font-size: 28rpx;color: #929292;">清空</span></view>
						<view class="list">
							<span :class="familylistIndex == index ? 'active' : ''" @click="checkFamily(index)"
								v-for="(item, index) in familylist" :key="index">{{ item.label }}</span>
						</view>
					</view>
					<!-- 家庭成员 -->
					<view class="his" v-if="userList.length > 0">
						<view class="title"><span>家庭成员</span><span @click="clearuserlist"
								style="font-size: 28rpx;color: #929292;">清空</span></view>
						<view class="list">
							<span :class="item.checked ? 'active' : ''" @click="checkuser(index)"
								v-for="(item, index) in userList"
								:key="index">{{ item.realname?item.realname:item.nickname }}</span>
						</view>
					</view>
				</view>

				<view class="moreSearch" @click="showMoreSearch = !showMoreSearch">
					更多筛选条件
					<i class="iconfont" :class="showMoreSearch ? 'upIcon' : ''">&#xe606;</i>
				</view>
			</view>

		</view>




		<w-picker :visible.sync="rangeVisible" mode="range" startYear="2020" endYear="2099" :current="true"
			@confirm="onConfirm($event)" ref="range"></w-picker>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgColor: '#de4849',
				titleStyle: {
					color: '#fff'
				},
				startDate: '', //正常日期
				endDate: '',
				userList: [],
				query: {
					searchContent: '',
					categoryIds: [],
					endDate: '', //时间戳格式
					startDate: '',
					pageSize: 10,
					familyId: '',
					familyAttenders: [],
					lastDocId: '',
					isDraft: false
				},
				familylistIndex: -1,
				familylist: [],
				tabTitle: [],
				hislist: uni.getStorageSync('hislist') ? uni.getStorageSync('hislist') : [],
				actionStyle: {
					color: '#fff'
				},
				showMoreSearch: false,
				rangeVisible: false,
				video: ''
			};
		},
		onLoad(e) {
			if (e.video) {
				this.video = e.video
			}
			this.getAllCategories();
			this.getMyfamilyData()
		},
		methods: {
			clearDate() {
				this.query.startDate = this.startDate = this.query.endDate = this.endDate = ''
			},
			clearType() {
				this.tabTitle.forEach(item => {
					item.checked = false
				})
			},
			clearuserlist() {
				this.query.familyAttenders = []

				if (this.userList.length > 0) {
					this.userList.forEach(item => {
						item.checked = false
					})
				}
			},
			clearFamily() {
				this.query.familyId = ''
				this.familylistIndex = -1
				this.userList = []
			},
			getMyfamilyData() {

				this.$api.myFamily().then(res => {
					let familylist = [];
					uni.setStorageSync('myFamilyIndexs', []);
					if (res.familyInfo) {
						for (let i in res.familyInfo) {
							familylist.push({
								familyId: i,
								family: res.familyInfo[i].family
							});
						}
						if (familylist.length > 0) {
							familylist.forEach((item, index) => {
								console.log(item);
								item.label = item.family.familyName;
								item.id = item.familyId;
								item.checked = false
							});
						}

						this.familylist = familylist
						console.log(this.familylist);
					}
				});
			},
			onConfirm(res) {
				this.startDate = this.trimSpecial(res.value[0])
				this.endDate = this.trimSpecial(res.value[1])
				console.log(this.startDate)
				console.log(this.endDate)
				console.log(res)
			},
			trimSpecial(string) {

				//替换字符串中的所有特殊字符（包含空格）
				if (string != "") {
					const pattern = /[`~!@#$^\-&*()=|{}':;',\\\[\]\.<>\/?~！@#￥……&*（）——|{}【】'；：""'。，、？\s]/g;
					string = string.replace(pattern, "");
				}
				return string
			},
			back() {
				uni.navigateBack();
			},
			clearHis() {
				this.hislist = [];
				uni.setStorageSync('hislist', []);
			},
			copyValue(data) {
				this.query.searchContent = data;
				this.goSearch();
			},
			addToHistory(){
				// 加入搜索历史
				let index = this.hislist.findIndex(v => v===this.query.searchContent)
				if(index !== -1){
					this.hislist.unshift(this.hislist.splice(index,1)[0])
				} else {
					this.hislist.unshift(this.query.searchContent)
				}
				uni.setStorageSync('hislist', this.hislist);
			},
			setHistory() {
				if (this.query.searchContent) {
					this.addToHistory()
				}
			},
			searchSubmit() {
				this.setHistory();
				this.goSearch();
			},
			goSearch() {
				console.log(this.query)

				let arr = []
				if (this.tabTitle.length > 0) {
					this.tabTitle.forEach(item => {
						if (item.checked) {
							arr.push(item.id)
						}
					})
				}

				this.query.categoryIds = arr

				let arr2 = []
				if (this.userList.length > 0) {
					this.userList.forEach(item => {
						if (item.checked) {
							arr2.push(item.id)
						}
					})
				}

				this.query.familyAttenders = arr2

				if (this.startDate) {
					this.query.startDate = this.startDate
					this.query.endDate = this.endDate
				}

				if (this.query.searchContent == '') {
					delete this.query.searchContent
				}

				let queryData = JSON.stringify(this.query)

				if (this.video) {
					uni.$emit('returnData', queryData)
					uni.navigateBack({
						delta: 1
					})

				} else {
					uni.navigateTo({
						url: `/pages/news/searchover?queryData=${queryData}`
					})
				}

			},
			getAllCategories() {
				let tabBars = [];
				this.$api.getAllCategories().then(res => {
					if (res) {
						for (let i in res) {
							tabBars.push({
								name: res[i],
								id: i,
								checked: false
							});
						}
						this.tabTitle = tabBars;
					}
				});
			},
			checkFenlei(index) {
				this.tabTitle[index].checked = !this.tabTitle[index].checked;
			},
			checkuser(index) {
				this.userList[index].checked = !this.userList[index].checked;
			},
			checkFamily(index) {
				this.familylistIndex = index

				let familyId = this.familylist[index].familyId
				this.query.familyId = familyId
				this.query.familyAttenders = []

				this.getFamUser(familyId)
			},
			getFamUser(familyId) {
				let data = {
					familyId
				}
				this.$api.queryFamilyMember(data).then(res => {
					console.log(res)

					if (res.list.length > 0) {
						res.list.forEach(item => {
							item.checked = false
						})
					}

					this.userList = res.list
					console.log(this.userList)


				})
			},
		}
	};
</script>

<style>
	page {
		background-color: #fff;
	}
</style>
<style lang="scss" scoped>
	.barHeight {
		height: var(--status-bar-height);
		background-color: #de4849;
	}


	.form {
		width: 100%;
		margin: 0 auto;
		border-radius: 20rpx;
		overflow: hidden;

	}

	.moreSearch {
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #7e7c7c;
		margin-top: 40rpx;
		display: flex;
		align-items: center;

		.iconfont {
			margin-left: 10rpx;
			font-size: 30rpx;
			display: block;
		}

		.upIcon {
			transform: rotate(180deg);
		}
	}

	.search {
		background-color: #f97f7f;
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 110rpx;
		padding: 0 30rpx;
		width: 100%;
		z-index: 100;
		color: #fff;

		.leftIcon {
			margin-right: 20rpx;
		}
	}

	.his {
		margin-top: 30rpx;
		padding: 0 20rpx;

		.title {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-bottom: 20rpx;

			span {
				font-size: 30rpx;
			}
		}

		.list {
			display: flex;
			align-items: center;
			flex-wrap: wrap;

			span {
				padding: 4px 20px;
				border-radius: 5px;
				background-color: #fff;
				border: 1px solid #b7b7b7;
				font-size: 26rpx;
				margin-right: 20rpx;
				margin-bottom: 20rpx;
				color: #7e7e7e;
			}

			.dateSpan {
				width: 220rpx;
				text-align: center;
				margin-bottom: 0;
			}

			.zhi {
				margin: 0 20rpx;
				font-size: 26rpx;
				color: #7e7e7e;
			}

			.active {
				background-color: #de4849;
				color: #fff;
				border-color: #de4849;
			}
		}
	}

	::v-deep .u-search__action {
		color: #ffffff !important;
		cursor: pointer;
	}
</style>